<?php
namespace app\site\controller;

use app\common\model\ShopGoodsBrandModel;
use app\common\model\ShopGoodsCategoryModel;
use think\Request;

class ShopGoodsBrandController extends BaseController
{

    private $goodsBrandModel;
    private $goodsCategoryModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->goodsBrandModel    = new ShopGoodsBrandModel();
        $this->goodsCategoryModel = new ShopGoodsCategoryModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map       = [];
        $query     = [];
        $map['site_id'] = $this->getSiteId();
        $map['brand_status'] = 0; //申请中品牌
        $brandName = $this->request->param('brand_name');
        if (!empty($brandName)) {
            $map['brand_name']   = ['like', '%' . $brandName . '%'];
            $query['brand_name'] = $brandName;
        }
        $list = $this->goodsBrandModel->where($map)->order('brand_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $data['brand_status'] = 0; //待审核
            $result = $this->validate($data, 'ShopGoodsBrandValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $cidPath = explode(',',$data['cid_path']);
            $cidPath = array_filter($cidPath);
            $cidNames = $this->goodsCategoryModel->where('cid','in',$cidPath)->column('cat_name');
            $data['cid_path'] = implode(',',$cidPath);
            $data['cid_names'] = implode(',',$cidNames);
            $this->goodsBrandModel->addData($data);
            $this->success('添加成功', url('index'));
        }
        $this->assign('goodsCategory', $this->goodsCategoryCache);
        return $this->fetch();
    }

    public function edit(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $result = $this->validate($data, 'ShopGoodsBrandValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $cidPath = explode(',',$data['cid_path']);
            $cidPath = array_filter($cidPath);
            $cidNames = $this->goodsCategoryModel->where('cid','in',$cidPath)->column('cat_name');
            $data['cid_path'] = implode(',',$cidPath);
            $data['cid_names'] = implode(',',$cidNames);
            $this->goodsBrandModel->editData($data);
            $this->success('添加成功', url('index'));
        }
        $brandId = $this->request->param('brand_id');
        $brandInfo = $this->goodsBrandModel->where('brand_id',$brandId)->find();
        $this->assign('info',$brandInfo);
        return $this->fetch();
    }
    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $brandId = $this->request->param('brand_id/d');
            $this->goodsBrandModel->delData($brandId);
            $this->success('删除成功！');
        }
    }

}
